package top.drinyeit.weilong.presenter;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.HashMap;
import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.SecondCategoryNewActivity;
import top.drinyeit.weilong.adapter.ClassificationAdapter;
import top.drinyeit.weilong.fragment.ClassificationFragment;
import top.drinyeit.weilong.model.ClassificationBean;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/11.
 */

public class ClassificationPresenter {
    private ClassificationFragment fragment;
    private ClassificationAdapter classificationAdapter;
    public ClassificationPresenter(ClassificationFragment fragment){
        this.fragment = fragment;
    }
    //获取全部分类信息
    public void getCla() {
        Map<String,Object> map = new HashMap<>();
        map.put("action","commodity_category");
        CommonSubscriber<HttpResponse<ClassificationBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<ClassificationBean>>() {
            @Override
            public void onError(String e, int code) {
                fragment.swipeRefreshLayout.setRefreshing(false);
                fragment.showToastMsg(e);
            }

            @Override
            public void onNext(final HttpResponse<ClassificationBean> o) {
                fragment.swipeRefreshLayout.setRefreshing(false);
                if (o.getCode() == COMMON_SUCCESS) {
                    if (o.getData().getBanner() != null && o.getData().getBanner().size() > 0){
                        Glide.with(fragment).load(o.getData().getBanner().get(0).getBanner_img_url())
                                .dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                                .placeholder(R.drawable.ic_def)
                                .into(fragment.carouselView);
                    }
                    classificationAdapter = new ClassificationAdapter(o.getData().getCategory());
                    fragment.mRecyclerView.setLayoutManager(new GridLayoutManager(fragment.getActivity(),3));
                    fragment.mRecyclerView.setAdapter(classificationAdapter);
                    classificationAdapter.setOnItemClickListener(new ClassificationAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(fragment.getActivity(), SecondCategoryNewActivity.class);
                            intent.putExtra("category", o.getData().getCategory().get(position));
                            intent.putExtra("type","classification");
                            fragment.startActivity(intent);
                        }
                    });
                }else {
                    fragment.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getCla(subscriber, map);
    }
}
